package service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjw.entity.LoginUser;
import com.hjw.entity.User;
import com.hjw.mapper.UserMapper;
import com.hjw.service.IUserService;
import com.hjw.vo.UserVo;

import java.util.UUID;

public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public LoginUser findUser(UserVo vo) {
        LoginUser loginUser = new LoginUser();
        //1.0 生成一个token值
        String token = UUID.randomUUID().toString();

        loginUser.setToken(token);
        //1.1 根据用户名查询用户对象
        User user = this.getOne(new QueryWrapper<User>().eq("username", vo.getUsername()));

        ///////////////////////////// 查询当前登录用户的权限与角色列表  /////////////////////////////
        //1.1 获取用户id
        String id = user.getId();

        //1.2 对vo中的密码（表单传入）进行加密
        String pwd = SecureUtil.md5(vo.getPassword());
        //1.3 比较与数据库是否一致
        if (pwd.equals(user.getPassword())) {
            loginUser.setUser(user);
            return loginUser;
        }
        return null;
    }
}
